#include "picshow.h"
#include "ui_picshow.h"

PicShow::PicShow(QWidget *parent)
    : QDialog(parent)
    , ui(new Ui::PicShow)
{
    ui->setupUi(this);
    ui->previousBtn->SetIcons(":/icon/previous.png", ":/icon/previous_hover.png", ":/icon/previous_press.png");
    ui->nextBtn->SetIcons(":/icon/next.png", ":/icon/next_hover.png", ":/icon/next_press.png");

    QGraphicsOpacityEffect *opacity_pre = new QGraphicsOpacityEffect(this);
    opacity_pre->setOpacity(0);     // 透明度设置为0
    ui->previousBtn->setGraphicsEffect(opacity_pre);

    QGraphicsOpacityEffect *opacity_next= new QGraphicsOpacityEffect(this);
    opacity_next->setOpacity(0);     // 透明度设置为0
    ui->nextBtn->setGraphicsEffect(opacity_next);

    _animation_show_pre = new QPropertyAnimation(opacity_pre, "opacity", this);
    _animation_show_pre->setEasingCurve(QEasingCurve::Linear);      // setEasingCurve()指定动画过程中属性值变化的速度和方式
    _animation_show_pre->setDuration(500);      // 动画的持续时间

    _animation_show_next = new QPropertyAnimation(opacity_next, "opacity", this);
    _animation_show_next->setEasingCurve(QEasingCurve::Linear);      // setEasingCurve()指定动画过程中属性值变化的速度和方式
    _animation_show_next->setDuration(500);      // 动画的持续时间

    connect(ui->nextBtn, &QPushButton::clicked, this, &PicShow::SigNextClicked);        // 后一个按钮被点击
    connect(ui->previousBtn, &QPushButton::clicked, this, &PicShow::SigPreClicked);        // 前一个按钮被点击
}

PicShow::~PicShow()
{
    delete ui;
}

void PicShow::ReloadPic()
{
    if(_selected_path != ""){
        const auto &width = ui->gridLayout->geometry().width();
        const auto &height = ui->gridLayout->geometry().height();
        _pix_map.load(_selected_path);
        _pix_map = _pix_map.scaled(width, height, Qt::KeepAspectRatio);
        ui->label->setPixmap(_pix_map);
    }
}

bool PicShow::event(QEvent *event)
{
    switch (event->type()) {
        case QEvent::Enter:
            showPreNextBtns(true);
            break;
        case QEvent::Leave:
            showPreNextBtns(false);
            break;
        default:
            break;
    }

        return QDialog::event(event);       // 重写后子类要调用基类的方法
}

void PicShow::showPreNextBtns(bool b_show)
{
    if(!b_show &&_b_btnvisible){        // 判断按钮是否显示以及是否可见
        _animation_show_pre->stop();        // 停止动画
        _animation_show_pre->setStartValue(1);      // 动画从显示变到隐藏
        _animation_show_pre->setEndValue(0);
        _animation_show_pre->start();

        _animation_show_next->stop();        // 停止动画
        _animation_show_next->setStartValue(1);      // 动画从显示变到隐藏
        _animation_show_next->setEndValue(0);
        _animation_show_next->start();

        _b_btnvisible = false;      // 显示为不可见状态

        return ;
    }

    if(b_show && !_b_btnvisible){        // 显示且不可见装填时
        _animation_show_pre->stop();        // 停止动画
        _animation_show_pre->setStartValue(0);      // 动画从显示变到隐藏
        _animation_show_pre->setEndValue(1);
        _animation_show_pre->start();

        _animation_show_next->stop();        // 停止动画
        _animation_show_next->setStartValue(0);      // 动画从显示变到隐藏
        _animation_show_next->setEndValue(1);
        _animation_show_next->start();

        _b_btnvisible = true;      // 显示为不可见状态

        return ;
    }
}

void PicShow::SlotSelectItem(const QString &path)
{
    _selected_path = path;
    _pix_map.load(path);        // 加载图片
    auto width = this->width()-20;
    auto height = this->height()-20;
    _pix_map = _pix_map.scaled(width, height, Qt::KeepAspectRatio);     // 图片等比例拉伸
    ui->label->setPixmap(_pix_map);
}

void PicShow::SlotUpdatePic(const QString &_path)
{
    _selected_path = _path;     // 更新路径
    if(_selected_path != ""){
        const auto &width = ui->gridLayout->geometry().width();
        const auto &height = ui->gridLayout->geometry().height();
        _pix_map.load(_selected_path);
        _pix_map = _pix_map.scaled(width, height, Qt::KeepAspectRatio);
        ui->label->setPixmap(_pix_map);
    }
}

void PicShow::SlotDeleteItem()
{
    _selected_path = "";
}
